package com.sxt.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.bus.domain.Goods;
import com.sxt.bus.domain.Provider;
import com.sxt.bus.domain.Salesback;
import com.sxt.bus.service.GoodsService;
import com.sxt.bus.service.ProviderService;
import com.sxt.bus.service.SalesbackService;
import com.sxt.bus.vo.SalesbackVo;
import com.sxt.opd.domain.Patient;
import com.sxt.opd.service.PatientService;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2019-12-26
 */
@RestController
@RequestMapping("/salesback")
public class SalesbackController {

    @Autowired
    private SalesbackService salesbackService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private ProviderService providerService;



    /**
     * 查询
     */
    @RequestMapping("loadAllSalesback")
    public DataGridView loadAllSalesback(SalesbackVo salesbackVo) {
        IPage<Salesback> page=new Page<>(salesbackVo.getPage(), salesbackVo.getLimit());
        QueryWrapper<Salesback> queryWrapper=new QueryWrapper<>();
        //其他字段查询
        queryWrapper.eq(salesbackVo.getPatientid()!=null&&salesbackVo.getPatientid()!=0,
                "patientid",salesbackVo.getPatientid());
        queryWrapper.eq(salesbackVo.getGoodsid()!=null&&salesbackVo.getGoodsid()!=0,
                "goodsid",salesbackVo.getGoodsid());
        List<Integer> listgoodsid = new ArrayList<Integer>();
        if(null!=salesbackVo.getProviderid()){
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("providerid",salesbackVo.getProviderid());
            List<Goods> list = goodsService.list(goodsQueryWrapper);
            for (Goods goods : list) {
                listgoodsid.add(goods.getId());
            }
        }
        queryWrapper.in(salesbackVo.getProviderid()!=null && salesbackVo.getProviderid()!=0,
                "goodsid",listgoodsid);

        //模糊查询
        queryWrapper.ge(salesbackVo.getStartTime()!=null,"salesbacktime",salesbackVo.getStartTime());
        queryWrapper.le(salesbackVo.getEndTime()!=null,"salesbacktime",salesbackVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(salesbackVo.getOperateperson()), "operateperson", salesbackVo.getOperateperson());
        queryWrapper.like(StringUtils.isNotBlank(salesbackVo.getRemark()), "remark", salesbackVo.getRemark());
        queryWrapper.orderByDesc("salesbacktime");
        this.salesbackService.page(page, queryWrapper);
        List<Salesback> records = page.getRecords();
        for (Salesback salesback : records) {
            Patient patient = this.patientService.getById(salesback.getPatientid());
            if(null!=patient){
                salesback.setProvidername(patient.getPatientname());
            }
            Goods goods = this.goodsService.getById(salesback.getGoodsid());
            if(null!=goods) {
                salesback.setGoodsname(goods.getGoodsname());
                salesback.setSize(goods.getSize());
                Provider  provider = this.providerService.getById(goods.getProviderid());
                if(null!=provider){
                    salesback.setProvidername(provider.getProvidername());
                    salesback.setProviderid(provider.getId());
                }
            }
        }
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 添加退货信息
     */
    @RequestMapping("addSalesback")
    public ResultObj addSalesback(Integer id, Integer number, String remark){
        try {
            this.salesbackService.addSalesback(id,number,remark);
            return ResultObj.OPERATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }
}



